package com.theydream.qeechain.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.papaya.social.PPYFeedView;
import com.papaya.social.PPYSession;
import com.papaya.social.PPYSocial;
import com.theydream.qeechain.R;
import com.theydream.qeechain.ARcaught.ARClick;
import com.theydream.qeechain.battle.Battle;
import com.theydream.qeechain.globaldata.GlobalData;
import com.theydream.qeechain.map.Map;
import com.theydream.qeechain.publicmethod.PublicMethod;

public class ActiveMenu extends Activity implements PPYSocial.Delegate {
	
		private PPYFeedView feedPPY;
		private static final int ID_ME     = 1;
		private static final int ID_MAP   = 2;
		private static final int ID_SEARCH = 3;
		private static final int ID_ITEM   = 4;
		private static final int ID_PET  = 5;
		private static final int ID_GAME  = 6;
		private static final int ID_EXCHANGE  = 7;	
		
		private Animation mapButton_Animation;
		private Animation doorButton_Animation;
		private Animation meButton_Animation;

		// ================================================================
		// Inherited methods
		// ================================================================

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.active_home);
			feedPPY=(PPYFeedView)findViewById(R.id.entrance);
			GlobalData.initialize(this.getApplicationContext());
			GlobalData.getInstance().getDbhelper().loadPetInfo();
			GlobalData.getInstance().getDbhelper().loadMapItem();
			GlobalData.getInstance().getDbhelper().loadCaughtPets();
			GlobalData.getInstance().getDbhelper().loadSkill();
			//Use this two line to set My Pet Image on Main Menu
			ImageView pet=(ImageView) findViewById(R.id.home_myqee);
			pet.setImageResource(getResources().getIdentifier(GlobalData.getInstance().getPetInUse().get(0).getPic()+"_right", "drawable", this.getPackageName()));
			//Set and run the animation of the Main menu
			ImageView homeBacklight=(ImageView) findViewById(R.id.home_sunlight);
			homeBacklight.setBackgroundResource(R.anim.home_backlight);
			final AnimationDrawable backLightAnimation = (AnimationDrawable) homeBacklight.getBackground();
			homeBacklight.post(new Runnable() {
			    public void run() {
			        if ( backLightAnimation != null ) backLightAnimation.start();
			      }
			});
//			ImageView homeSun=(ImageView) findViewById(R.id.home_sun);
//			homeSun.setBackgroundResource(R.anim.home_sun);
//			final AnimationDrawable animationSun = (AnimationDrawable) homeSun.getBackground();
//			homeSun.post(new Runnable() {
//			    public void run() {
//			        if ( animationSun != null ) animationSun.start();
//			      }
//			});
//			ImageView homeMap=(ImageView) findViewById(R.id.home_map);
//			homeMap.setBackgroundResource(R.anim.home_map);
//			final AnimationDrawable animationMap = (AnimationDrawable) homeMap.getBackground();
//			homeSun.post(new Runnable() {
//			    public void run() {
//			        if ( animationMap != null ) animationMap.start();
//			      }
//			});
//			ImageView homeQee=(ImageView) findViewById(R.id.home_qee);
//			homeQee.setBackgroundResource(R.anim.home_qee);
//			final AnimationDrawable animationQee = (AnimationDrawable) homeQee.getBackground();
//			homeQee.post(new Runnable() {
//			    public void run() {
//			        if ( animationQee != null ) animationQee.start();
//			      }
//			});
//			ImageView homeMyQee=(ImageView) findViewById(R.id.home_myqee);
//			homeMyQee.setBackgroundResource(R.anim.home_myqee);
//			final AnimationDrawable animationMyQee = (AnimationDrawable) homeMyQee.getBackground();
//			homeMyQee.post(new Runnable() {
//			    public void run() {
//			        if ( animationMyQee != null ) animationMyQee.start();
//			      }
//			});
			
			final ImageView mapButton=(ImageView) findViewById(R.id.home_map);
			mapButton_Animation=AnimationUtils.loadAnimation(this, R.anim.home_button_effect);
			mapButton.startAnimation(mapButton_Animation);
			final ImageView qeeButton=(ImageView) findViewById(R.id.home_qee);
			qeeButton.startAnimation(mapButton_Animation);
			mapButton_Animation.setAnimationListener(new Animation.AnimationListener() {
				public void onAnimationEnd(Animation animation) {
					mapButton_Animation=AnimationUtils.loadAnimation(ActiveMenu.this, R.anim.home_button_effect);
					mapButton_Animation.setAnimationListener(this);
					mapButton.startAnimation(mapButton_Animation);
					qeeButton.startAnimation(mapButton_Animation);
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
				}
		});
			
			final ImageView doorButton=(ImageView) findViewById(R.id.home_door);
			doorButton_Animation=AnimationUtils.loadAnimation(this, R.anim.home_door_effect);
			doorButton.startAnimation(doorButton_Animation);
			doorButton_Animation.setAnimationListener(new Animation.AnimationListener() {
				public void onAnimationEnd(Animation animation) {
					doorButton_Animation=AnimationUtils.loadAnimation(ActiveMenu.this, R.anim.home_door_effect);
					doorButton_Animation.setAnimationListener(this);
					doorButton.startAnimation(doorButton_Animation);
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
				}
		});
			final ImageView meButton=(ImageView) findViewById(R.id.home_myqee);
			meButton_Animation=AnimationUtils.loadAnimation(this, R.anim.home_myqee_effect);
			meButton.startAnimation(meButton_Animation);
			meButton_Animation.setAnimationListener(new Animation.AnimationListener() {
				public void onAnimationEnd(Animation animation) {
					meButton_Animation=AnimationUtils.loadAnimation(ActiveMenu.this, R.anim.home_door_effect);
					meButton_Animation.setAnimationListener(this);
					meButton.startAnimation(doorButton_Animation);
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
				}
		});
			
//			ImageView homeDoor=(ImageView) findViewById(R.id.home_door);
//			homeDoor.setBackgroundResource(R.anim.home_door);
//			final AnimationDrawable animationDoor = (AnimationDrawable) homeDoor.getBackground();
//			homeDoor.post(new Runnable() {
//			    public void run() {
//			        if ( animationDoor != null ) animationDoor.start();
//			      }
//			});
			ImageView homeSea=(ImageView) findViewById(R.id.home_sea);
			homeSea.setBackgroundResource(R.anim.home_sea);
			final AnimationDrawable animationSea = (AnimationDrawable) homeSea.getBackground();
			homeSea.post(new Runnable() {
			    public void run() {
			        if ( animationSea != null ) animationSea.start();
			      }
			});

			//checkInternetConnection() ;
			PPYSocial.addDelegate(this);
			
		
			ActionItem homeItem 	= new ActionItem(ID_ME, "Me", getResources().getDrawable(R.drawable.action_news));
	        ActionItem mapItem 	= new ActionItem(ID_MAP, "Map", getResources().getDrawable(R.drawable.action_map));
			ActionItem searchItem 	= new ActionItem(ID_SEARCH, "Search", getResources().getDrawable(R.drawable.action_radar));
	        ActionItem itemItem 	= new ActionItem(ID_ITEM, "Item", getResources().getDrawable(R.drawable.action_item));
	        ActionItem petItem 	= new ActionItem(ID_PET, "Pet", getResources().getDrawable(R.drawable.action_pet));
	        ActionItem gameItem 	= new ActionItem(ID_GAME, "Game", getResources().getDrawable(R.drawable.action_game));
	        ActionItem exchangeItem 	= new ActionItem(ID_EXCHANGE, "Exchange", getResources().getDrawable(R.drawable.action_exchange));

	        final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);
			
			//add action items into QuickAction
	        quickAction.addActionItem(homeItem);
			quickAction.addActionItem(mapItem);
	        quickAction.addActionItem(searchItem);
	        quickAction.addActionItem(itemItem);
	        quickAction.addActionItem(petItem);
	        quickAction.addActionItem(gameItem);
	        quickAction.addActionItem(exchangeItem);
	      //Set listener for action item clicked
			quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {			
				@Override
				public void onItemClick(QuickAction source, int pos, int actionId) {				
					ActionItem actionItem = quickAction.getActionItem(pos);
					//here we can filter which action item was clicked with pos or actionId parameter
					if (actionId == ID_ME) {
						toMyQee(null);
					} else if (actionId == ID_MAP) {
						toMap(null);
					}
					else if (actionId == ID_SEARCH) {
						toAR(null);
					}
					else if (actionId == ID_ITEM) {
						Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
					}
					else if (actionId == ID_PET) {
						Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
					}
					else if (actionId == ID_GAME) {
						Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
					}
					else {
						Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
					}
				}
			});
			ImageView mainButton=(ImageView) findViewById (R.id.main_button);
			mainButton.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					quickAction.show(v);
					
				}
			});
		}
		
		@Override
		public void onResume()	{
			super.onResume();
			ImageView pet=(ImageView) findViewById(R.id.home_myqee);
			pet.setImageResource(getResources().getIdentifier(GlobalData.getInstance().getPetInUse().get(0).getPic()+"_right", "drawable", this.getPackageName()));
			feedPPY.setRunThread(true);
			
		}
		@Override
		protected void onDestroy() {
			super.onDestroy();
			PPYSocial.removeDelegate(this);
		}

		protected void onPause() {
			super.onPause();
			feedPPY.setRunThread(false);
		}

		public void onSessionUpdated() {
			toastSession();
		}

		public void onScoreUpdated() {
			toastSession();
		}

		public void onAccountChanged(int oldUID, int newUID) {

		}
		private void toastSession() {
			PPYSession session = PPYSession.getInstance();
			String msg = String.format(getResources().getString(R.string.text_session_toast), session.getUID(),
					session.getNickname(), session.getScores());
			Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
		}
		public void toMap(View target)
		{
			Intent intent=new Intent(this,Map.class);
			startActivity(intent);
		}
		public void toAR(View target)
		{
			Intent intent=new Intent(this,ARClick.class);
			startActivity(intent);
		}
		public void toCollection(View target)
		{
			Intent intent=new Intent(this,Collected.class);
			startActivity(intent);
		}
		public void toMyQee(View target)
		{
			Intent intent=new Intent(this,MyPet.class);
			startActivity(intent);
		}
		public void toTutorial(View target)
		{
			Intent intent=new Intent(this,Tutorial.class);
			startActivity(intent);
		}
		
		public void toBoss(View target)
		{
			if (PublicMethod.canFightBoss())
			{
				Intent intent=new Intent(this,Battle.class);
				GlobalData.getInstance().setArID(Integer.toString(GlobalData.getInstance().getPetList().size()));
				Bundle bundle = new Bundle();
				bundle.putString("eid", Integer.toString(GlobalData.getInstance().getPetList().size()));
				intent.putExtras(bundle);
				startActivity(intent);
			}
			else
			{
				Intent intent=new Intent(this,BossPage.class);
				startActivity(intent);
			}
			
		}
		public void checkInternetConnection() 
		{
		    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
		    // test for connection
		    if (cm.getActiveNetworkInfo() != null
		            && cm.getActiveNetworkInfo().isAvailable()
		            && cm.getActiveNetworkInfo().isConnected()) {
		    	
		        
		    } 
		    else {
		        Log.i("Connection", "Internet Connection Not Present");
					AlertDialog.Builder builder = new AlertDialog.Builder(this);
					builder.setMessage("3G or WiFi Network Connection Needed")
					       .setCancelable(true)
					       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					        	   dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
		    }
		}

}
